GBML manual / Grids

grid_filter

Makes a new LIST of just the cells of target that the script test says yes to, going row by row from the top left, in that order. test is the NAME of one of your scripts; it takes one argument (the cell's value) and gives back true or false. The answer is a list, not a grid: the cells that pass no longer have places to sit in. target is not changed.

Syntax

grid_filter(target, test) -> list<T>

Arguments

NameType
targetgrid
testscript

Returns

list<T>

Description

Makes a new list holding only the cells of target, a grid, that a script says yes to. The script is asked about each cell's value in turn, row by row: the whole top row from left to right, then the next row down, and so on. It answers true to keep the value and false to leave it out. The values that are kept come in that same order. target itself is not changed.

var high = grid_filter(board, is_high);

The answer is a plain list of values. It does not remember which cell each value came from, and the script is handed only the value, not where the cell is. If you need the places, loop over the grid yourself with board[# x, y].

The script

The second thing you give is the name of one of your scripts: a file in the project's scripts folder with a function named like the file. Write just the name, with no brackets after it. Here is a script that answers whether a score is high:

// scripts/is_high.gbml
function is_high(score: int): bool {
    return score >= 50;
}

The script has to follow four rules, and each one is checked before the game runs:

A function written out in place

You do not have to make a script file for a short job. Write the function right where the name would go:

var high = grid_filter(board, function (n) { return n >= 50; });

It is the script, only without a name. Its one parameter takes the kind of the cells of the grid, so you do not write the kind (you may, and then it has to fit). It can read and change the variables around it, such as a local you made on the line before, and a return inside it leaves only the function, never the event or script around it. Use a script file instead when the same job is needed in more than one place.

It gives back a list, not a grid

The answer is a list. How many cells pass is only found out while the game runs, and the cells that pass no longer have rows and columns to sit in. A list is the container made for exactly this: it has a fixed capacity and a length that goes up and down. So you ask the answer how many passed with list_length.

How big the new list is

The new list has room for as many values as the grid has cells (the width times the height), because the most that could pass is every cell. A 10 by 8 grid makes a list with room for 80, even if only 3 cells pass. So a filter costs a whole list's worth of memory. If the grid is a variable of an object, every instance of the object carries its own.

For the same reason a filter can only be written in one place: the whole right-hand side of the line that makes the variable, like var high = grid_filter(board, is_high);, or high = grid_filter(board, is_high); in an object's Create event. It cannot go inside another call or a sum, on a line by itself, over a variable that already exists, or in a later event such as Step.

It also needs to know how big the grid you filter is, so that has to be a plain variable name whose size is known. A script's grid parameter is refused, because the caller decides how big that grid is. A filter cannot be given a size of its own either.

What happens in the awkward cases

You do thisWhat happens
the script says no to every cellthe new list is empty
the script says yes to every cellthe new list holds all of them, row by row
a grid of text, structs or referencesworks. Nothing is compared, the script decides
give a script that is not a script's name, has the wrong number of parameters, the wrong kind of parameter, or does not answer true or falserefused when checked, and the message says which
give it a list or an arrayrefused when checked, and the message names the function for that kind
use it where a value is needed, other than as the whole right-hand side of the line that makes a variablerefused when checked

What it costs

One call of your script for each cell of the grid, and a whole list of memory with room for as many values as the grid has cells. The grid is not changed.

Example

var board = grid_create(3, 2, 0);
board[# 0, 0] = 30;
board[# 1, 0] = 80;
board[# 2, 0] = 55;
board[# 0, 1] = 10;
var high = grid_filter(board, is_high);   // a list holding 80, 55
var how_many = list_length(high);         // 2

On the GBA

A fixed-size vector of W*H cells - the same size as the grid, since every cell could pass - filled by a loop over every cell, which a grid stores row by row. The script is handed only the value, not where the cell is. The answer is a LIST, not an array: how many items pass is only found out while the game runs, and an array's size is fixed when the game is built, whereas a list has a fixed capacity and a length that changes. Its capacity is the original's (the most that could pass is all of them), so it is a second whole container of memory: as many places as the original has room for, not just how many pass. Like a copy, it has to be the whole right-hand side of the line that makes the variable, the original has to be a variable whose size is known (a script's parameter could be any size), and it cannot be given a size of its own. The script is called once for each item, in order, and the items it says yes to are added to the answer in the same order; the original is not changed. The script has to give back true or false, take exactly one argument whose type accepts the items, and be the NAME of one of your scripts: a function written out in the middle of the line is not turned into game code yet, so it is refused instead of doing nothing. O(n) calls of the script, and no timing was measured.

See also: list_filterarray_filtergrid_foreachgrid_copygrid_createlist_lengthGridsScriptsWriting a scriptData containers